<template>
  <div class="divBox">
    <el-card class="box-card">
      <div slot="header" class="clearfix flex_wrap">
        <el-form inline size="small" :model="tableFrom" ref="tableFrom" label-width="100px">
          <el-col :xs="24" :sm="24" :md="24" :lg="20" :xl="20">
            <el-form-item label="数据场：">
              <UserSearchable v-model="tableFrom.dataFactoryId" :type="'datafieldList'" class="w_200">
                <template #dropdownTitle>
                  <span>数据场名称</span>
                  <span>负责人</span>
                </template>
                <template v-slot:dropdown="{ item }">
                  <span>{{ item.name }}</span>
                  <span>{{ item.principal }}</span>
                </template>
              </UserSearchable>
            </el-form-item>
            <el-form-item label="用户：">
              <UserSearchable v-model="tableFrom.userId" class="w_200"> </UserSearchable>
            </el-form-item>
            <el-form-item label="分组">
              <el-select v-model="tableFrom.groupId" placeholder="请选择分组" clearable class="w_120">
                <el-option :value="item.id" v-for="(item, index) in groupList" :key="index"
                  :label="item.groupName"></el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="标签">
              <el-select multiple v-model="searchFrom.tagId" placeholder="请选择标签" clearable class="w_250">
                <el-option :value="item.id" v-for="(item, index) in labelLists" :key="index"
                  :label="item.name"></el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="开始时间：">
              <el-date-picker v-model="tableFrom.startTime" type="date" value-format="yyyy-MM-dd" format="yyyy-MM-dd"
                placeholder="选择日期" clearable class="selWidth">
              </el-date-picker>
            </el-form-item>
            <el-form-item label="结束时间：">
              <el-date-picker v-model="tableFrom.endTime" type="date" value-format="yyyy-MM-dd" format="yyyy-MM-dd"
                placeholder="选择日期" clearable class="selWidth">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="24" :md="24" :lg="4" :xl="4">
            <el-form-item>
              <el-button class="mr10" size="small" type="primary" icon="el-icon-search" @click="search()">搜索</el-button>
              <el-button class="mr10" size="small" icon="el-icon-refresh" @click="reset()">重置</el-button>
            </el-form-item>
          </el-col>
        </el-form>
        <div>
          <el-button type="primary" size="mini" @click="adoptAll">通过</el-button>
          <el-button type="danger" size="mini" @click="noAdoptAll">拒绝</el-button>
        </div>
      </div>
      <el-table v-loading="listLoading" :data="tableData.data" style="width: 100%" size="mini" class="table"
        highlight-current-row :row-class-name="tableRowClassName" @select="selctTable" @select-all="slectTableAll">
        <el-table-column type="selection" align="center" min-width="100">
        </el-table-column>
        <el-table-column prop="id" label="ID" min-width="50" />
        <el-table-column label="头像" min-width="80">
          <template slot-scope="scope">
            <div class="demo-image__preview">
              <el-image :src="scope.row.avatar" />
            </div>
          </template>
        </el-table-column>
        <el-table-column prop="nickName" label="用户昵称/UID" min-width="150">
          <template slot-scope="scope">
            <span>{{ scope.row.nickName }}/{{ scope.row.userId }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="dataFactoryName" label="数据场" min-width="100" />
        <el-table-column prop="inOutType" label="类型" min-width="100">
          <template slot-scope="scope">
            <span v-if="scope.row.inOutType == 1">入额</span>
            <span v-if="scope.row.inOutType == 2">出额</span>
          </template>
        </el-table-column>
        <el-table-column prop="currencyName" label="币种" min-width="100" />

        <!-- <el-table-column prop="amount" label="金额" min-width="100" ></el-table-column> -->
        <el-table-column prop="amount" label="金额" min-width="100">
          <template slot-scope="scope">
            <span>{{
              parseFloat(`${scope.row.amount}`)
                .toFixed(2)
                .replace(/\B(?=(\d{3})+(?!\d))/g, ",")
            }}</span>
          </template>
        </el-table-column>


        <el-table-column prop="approvalStatus" label="状态" min-width="100">
          <template slot-scope="scope">
            <span v-if="scope.row.approvalStatus == 1">待审核</span>
            <span v-if="scope.row.approvalStatus == 2">已通过</span>
            <span v-if="scope.row.approvalStatus == 3">已拒绝</span>
          </template>
        </el-table-column>
        <el-table-column prop="createTime" label="创建时间" min-width="200" />
        <el-table-column prop="approvalDate" label="审核时间" min-width="200" />
        <el-table-column label="操作" min-width="130" fixed="right" align="center">
          <template slot-scope="scope">
            <el-button type="primary" size="mini" v-if="scope.row.approvalStatus == 1"
              @click="handlePass(scope.row)">通过</el-button>
            <el-button type="danger" size="mini" v-if="scope.row.approvalStatus == 1"
              @click="handleRefuse(scope.row)">拒绝</el-button>
            <el-button type="text" size="mini" v-if="scope.row.approvalStatus == 2">已通过</el-button>
            <el-button type="text" size="mini" v-if="scope.row.approvalStatus == 3">已拒绝</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="block">
        <el-pagination :page-sizes="[20, 40, 60, 80]" :page-size="tableFrom.limit" :current-page="tableFrom.page"
          layout="total, sizes, prev, pager, next, jumper" :total="tableData.total" @size-change="handleSizeChange"
          @current-change="pageChange" />
      </div>
    </el-card>

    <!--用户列表-->
    <el-dialog title="用户列表" :visible.sync="userVisible" width="900px" :close-on-click-modal="false"
      :close-on-press-escape="false">
      <user-list v-if="userVisible" @getTemplateRow="getTemplateRow"></user-list>
      <span slot="footer" class="dialog-footer">
        <el-button @click="userclose()">取 消</el-button>
        <el-button type="primary" @click="usersubmit()">确 定</el-button>
      </span>
    </el-dialog>


    <!-- 通过/拒绝弹窗 -->
    <el-dialog :title="adoptFlag == true ? '确认通过？' : '确认拒绝？'" :visible.sync="diagVisible" width="25%"
      :class="adoptFlag == true ? 'adoptdialog' : 'noadoptdialog'">

      <div class="adoptText">
        <div class="w_30">备注:</div>
        <el-input placeholder="请输入备注内容(必填)" v-model="adoptInput" clearable class="w_70">
        </el-input>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button size="small" @click="diagVisible = false">取 消</el-button>
        <el-button size="small" type="primary" @click="sureAdopt(singleType)">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { walletbillRechargeList, walletbillCheck } from "@/api/financial";
import UserSearchable from "@/components/UserSearchable";
import userList from "@/components/userList";

import {
  tagListApi,
  groupListApi
} from '@/api/user'

export default {
  components: {
    userList,
    UserSearchable,
  },
  data() {
    return {
      groupList: [],
      labelLists: [],
      tableFrom: {
        groupId: "",
        tagId: "",
        dataFactoryId: "",
        userId: "",
        startTime: "",
        endTime: "",
        page: 1,
        limit: 20,
      },
      searchFrom: {
        tagId: [],
      },


      userVisible: false,
      cardLists: [],
      listLoading: false,
      tableData: {
        data: [],
        total: 0,
      },

      diagVisible: false,
      adotList: [],
      adoptInput: "",
      adoptFlag: true,
      adoptObj: {},
      // 1 为全部通过，2为全部拒绝，3为单个通过，4为单个拒绝
      singleType: 1
    };
  },
  mounted() {
    this.getlist();
    // 分组、标签
    this.groupLists();
    this.getTagList();
  },
  methods: {
    // 分组列表
    groupLists() {
      groupListApi({ page: 1, limit: 9999 }).then(async (res) => {
        this.groupList = res.list;
      });
    },
    //标签列表
    getTagList() {
      tagListApi({ page: 1, limit: 9999 }).then((res) => {
        this.labelLists = res.list;
      });
    },



    selctTable(selection) {
      let that = this;
      console.log(selection)
      that.adotList = selection
    },
    slectTableAll(selection) {
      let that = this;
      console.log(selection)
      that.adotList = selection
    },
    adoptAll() {
      let that = this;
      if (that.adotList.length < 1) {
        return that.$message.warning('请至少选择一项！')
      }
      that.singleType = 1;
      that.adoptInput = "";
      that.adoptFlag = true;
      that.diagVisible = true;
      console.log(that.adotList)
    },
    noAdoptAll() {
      let that = this;
      if (that.adotList.length < 1) {
        return that.$message.warning('请至少选择一项！')
      }
      that.singleType = 2;
      that.adoptInput = "";
      that.adoptFlag = false;
      that.diagVisible = true;
    },
    sureAdopt() {
      let that = this;
      // console.log('标志：',that.adoptFlag)
      // console.log(that.adotList)
      if (that.adoptInput == "") {
        return that.$message.warning('请输入备注！')
      }
      if (that.singleType == 1) {
        let ids = []
        for (let i = 0; i < that.adotList.length; i++) {
          ids.push(that.adotList[i].id)
        }
        let data = {
          ids: ids,
          approvalStatus: 2,
          approvalRemark: that.adoptInput
        }
        console.log(data)
        that.adoptApi(data, '通过！', false)
      }
      if (that.singleType == 2) {
        let ids = []
        for (let i = 0; i < that.adotList.length; i++) {
          ids.push(that.adotList[i].id)
        }
        let data = {
          ids: ids,
          approvalStatus: 3,
          approvalRemark: that.adoptInput
        }
        console.log(data)
        that.adoptApi(data, '拒绝！', false)
      }
      if (that.singleType == 3) {
        let data = that.adoptObj
        data.approvalRemark = that.adoptInput
        that.adoptApi(data, '通过！', false)
      }
      if (that.singleType == 4) {
        let data = that.adoptObj
        data.approvalRemark = that.adoptInput
        that.adoptApi(data, '拒绝！', false)
      }

    },
    adoptApi(data, text, show) {
      walletbillCheck(data)
        .then((res) => {
          console.log(res);
          this.$message.success(text);
          this.diagVisible = show
          this.getlist();
        })
        .catch((err) => {
          console.log(err);
        });
    },
    tableRowClassName({ row, rowIndex }) {
      // if (row.warning == 2) {
      //   return "green-row";
      // }
      // if (row.warning == 3) {
      //   return "warning-row";
      // }
      // return "";

      if (row.amount >= row.remainBalance && row.amount < 2 * row.remainBalance) {
        return "green-row";
      }

      if (row.amount >= 2 * row.remainBalance || row.remainBalance < 0) {
        return "warning-row";
      }
      if (row.remainBalance > row.amount) {
        return ""
      }
    },
    selectUser() {
      this.userVisible = true;
    },
    getTemplateRow(row) {
      this.tableFrom.userId = row.uid;
    },
    userclose() {
      this.tableFrom.userId = "";
      this.userVisible = false;
    },
    usersubmit() {
      this.userVisible = false;
    },
    search() {
      this.tableFrom.page = 1;
      this.tableFrom.limit = 20;

      if (this.searchFrom.tagId.length > 0) {
        let tagId = this.searchFrom.tagId.join(",")
        this.tableFrom.tagId = tagId
      }

      this.getlist();
    },
    reset() {
      this.tableFrom = {
        tagId: "",
        groupId: "",
        dataFactoryId: "",
        userId: "",
        startTime: "",
        endTime: "",
        page: 1,
        limit: 20,
      };
      this.searchFrom.tagId = []
      this.getlist();
    },
    getlist() {
      this.listLoading = true;
      walletbillRechargeList(this.tableFrom)
        .then((res) => {
          console.log("出入金列表", res);
          this.tableData.data = res.list;
          this.tableData.total = res.total;
          this.listLoading = false;
        })
        .catch(() => {
          this.listLoading = false;
        });
    },
    handlePass(row) {
      console.log("通过", row);


      // walletbillCheck
      let that = this;
      that.singleType = 3;
      that.adoptInput = "";
      that.adoptFlag = true;
      that.diagVisible = true;

      let data = {
        ids: [row.id],
        approvalStatus: 2,
        approvalRemark: ""
      };

      that.adoptObj = data;

      // this.$confirm("确认通过？")
      //   .then((_) => {
      //     let data = {
      //       id: row.id,
      //       approvalStatus: 2,
      //     };
      //     walletbillCheck(data)
      //       .then((res) => {
      //         console.log(res);
      //         this.$message.success("通过");
      //         this.getlist();
      //       })
      //       .catch((err) => {
      //         console.log(err);
      //         this.$message.error(err);
      //       });
      //   })
      //   .catch((_) => { });
    },
    handleRefuse(row) {
      console.log("拒绝", row);
      let that = this;
      that.singleType = 4;
      that.adoptInput = "";
      that.adoptFlag = false;
      that.diagVisible = true;

      let data = {
        ids: [row.id],
        approvalStatus: 3,
        approvalRemark: ""
      };

      that.adoptObj = data;
      // this.$confirm("确认拒绝？")
      //   .then((_) => {
      //     let data = {
      //       id: row.id,
      //       approvalStatus: 3,
      //     };
      //     walletbillCheck(data)
      //       .then((res) => {
      //         console.log(res);
      //         this.$message.success("拒绝");
      //         this.getlist();
      //       })
      //       .catch((err) => {
      //         console.log(err);
      //         this.$message.error(err);
      //       });
      //   })
      //   .catch((_) => { });
    },
    pageChange(page) {
      this.tableFrom.page = page;
      this.getlist();
    },
    handleSizeChange(val) {
      this.tableFrom.limit = val;
      this.getlist();
    },
  },
};
</script>

<style lang="scss" scoped>
>>>.el-table .warning-row {
  color: #ff0000 !important;
}

>>>.el-table .green-row {
  color: green !important;
}

>>>.adoptdialog .el-dialog__title {
  color: green !important;
  font-size: 18px;
  font-weight: bold;
}

>>>.noadoptdialog .el-dialog__title {
  color: #ff0000 !important;
  font-size: 18px;
  font-weight: bold;
}

.adoptText {
  display: flex;
  justify-content: space-evenly;
  align-items: center;
}

.adoptText .w_30 {
  width: 15%;
}

.adoptText .w_70 {
  width: 80%;
}

// style code
.flex_wrap {
  display: flex;
  flex-wrap: wrap;
}

.w-60 {
  width: 60%;
}

.w_200 {
  width: 220px;
}

.w_120 {
  width: 120px;
}

.w_250 {
  width: 250px;
}
</style>
